/**
 * 
 */
package org.mspring.mlog.service.impl;

import org.mspring.mlog.entity.Comment;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.Stat;
import org.mspring.mlog.entity.constants.CacheNames;
import org.mspring.mlog.service.StatService;
import org.mspring.platform.cache.service.CacheService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Gao Youbo
 * @since 2012-8-14
 * @Description
 * @TODO
 */
@Service
@Transactional
public class StatServiceImpl extends GenericDao<Stat, java.lang.Long> implements StatService {

    @Autowired
    private CacheService cacheService;

    @Override
    public int getPostCount() {
        // TODO Auto-generated method stub
        Stat stat = getSingle(Stat.Type.POST_COUNT);
        if (stat != null && StringUtils.isNotBlank(stat.getValue())) {
            return Integer.parseInt(stat.getValue());
        }
        return 0;
    }

    @Override
    public int getReplys() {
        // TODO Auto-generated method stub
        Stat stat = getSingle(Stat.Type.COMMENT_COUNT);
        if (stat != null && StringUtils.isNotBlank(stat.getValue())) {
            return Integer.parseInt(stat.getValue());
        }
        return 0;
    }

    @Override
    public int getClickCount() {
        // TODO Auto-generated method stub
        Stat stat = getSingle(Stat.Type.CLICK_COUNT);
        if (stat != null && StringUtils.isNotBlank(stat.getValue())) {
            return Integer.parseInt(stat.getValue());
        }
        return 0;
    }

    @Override
    public void updatePostCount() {
        // TODO Auto-generated method stub
        Integer count = getHibernateTemplate().count("select count(*) from Post post where post.status = ?", Post.Status.PUBLISH);
        if (count != null) {
            Stat stat = new Stat(Stat.Type.POST_COUNT, count.toString());
            save(stat);
        }
    }

    @Override
    public void updateReplys() {
        // TODO Auto-generated method stub
        Integer count = getHibernateTemplate().count("select count(*) from Comment comment where comment.status = ?", Comment.Status.APPROVED);
        if (count != null) {
            Stat stat = new Stat(Stat.Type.COMMENT_COUNT, count.toString());
            save(stat);
        }
    }

    @Override
    public void updateClickCount() {
        // TODO Auto-generated method stub
        int value = getClickCount() + 1;
        Stat stat = new Stat(Stat.Type.CLICK_COUNT, String.valueOf(value));
        super.save(stat);
    }

    private Stat getSingle(final String type) {
        String key = "stat_" + type;
        Stat stat = cacheService.getCacheValue(CacheNames.DEFAULT_CACHE, key);
        if (stat == null) {
            stat = uniqueResult("select s from Stat s where s.type = ? order by id desc", type);
            cacheService.updateCacheValue(CacheNames.DEFAULT_CACHE, key, stat, CacheService.ONE_MINUTE * 10);
        }
        return stat;
    }

}
